package org.jay.angla.utils;

import java.util.Arrays;

import org.jay.angla.activity.QASettingActivity_;
import org.jay.angla.activity.TSSettingActivity_;
import org.jay.angla.activity.WSSettingActivity_;
import org.json.JSONArray;

import android.content.Context;
import android.content.SharedPreferences;

public class SharePreferenceUtil {
	// quantitative analysis setting
	public static final String QA_FITTING_METHOD_KEY = "FITTING_METHOD";
	public static final String QA_CONC_UNIT_KEY = "CONC_UNIT";
	public static final String QA_CALC_TYPE_KEY = "CALC_TYPE";
	public static final String QA_SAMPLE_NUM_KEY = "SAMPLE_KEY";
	public static final String QA_K0_KEY = "K0";
	public static final String QA_K1_KEY = "K1";
	public static final String QA_K2_KEY = "K2";
	public static final String QA_K3_KEY = "K3";
	public static final String QA_WAVELENGTH_NUM_KEY = "WAVELENGTH_NUM";
	public static final String QA_WAVELENGTH1_KEY = "WAVELENGTH1";
	public static final String QA_WAVELENGTH2_KEY = "WAVELENGTH2";
	public static final String QA_WAVELENGTH3_KEY = "WAVELENGTH3";
	public static final String QA_RATIO1_KEY = "RATIO1";
	public static final String QA_RATIO2_KEY = "RATIO2";
	public static final String QA_RATIO3_KEY = "RATIO3";
	// time scanning setting
	public static final String TS_TEST_MODE = "TS_TEST_MODE";
	public static final String TS_WAVE_LENGTH = "TS_WAVELENGTH";
	public static final String TS_START_TIME = "TS_START_TIME";
	public static final String TS_END_TIME = "TS_END_TIME";
	public static final String TS_INTERVAL = "TS_INTERVAL";
	public static final String TS_UP_LIMIT = "TS_UP_LIMIT";
	public static final String TS_DOWN_LIMIT = "TS_DOWN_LIMIT";
	// wavelength scanning setting
	public static final String WS_TEST_MODE = "WS_TEST_MODE";
	public static final String WS_UP_LIMIT = "WS_UP_LIMIT";
	public static final String WS_DOWN_LIMIT = "WS_DOWN_LIMIT";
	public static final String WS_START_WAVELENGTH = "WS_START_WL";
	public static final String WS_END_WAVELENGTH = "WS_END_WL";
	public static final String WS_WAVELENGTH_INTERVAL = "WS_WL_INTERVAL";

	// set data accuracy
	public static final String ACCURACY = "ACC";
	public static final int LOW_ACC = 0;
	public static final int HIGH_ACC = 1;

	// baseline
	public static final int BASELINE_LENGTH = 911;
	public static final String BASELINE_KEY = "BASELINE";

	// last bluetooth info
	public static final String BT_NAME_KEY = "BT_NAME";
	public static final String BT_ADDRESS_KEY = "BT_ADDRESS";

	private SharedPreferences sp;
	private SharedPreferences.Editor editor;

	public SharePreferenceUtil(Context context, String file) {
		sp = context.getSharedPreferences(file, Context.MODE_PRIVATE);
		editor = sp.edit();
	}

	public void setFittingMethod(int value) {
		editor.putInt(QA_FITTING_METHOD_KEY, value);
		editor.commit();
	}

	public int getFittingMethod() {
		return sp.getInt(QA_FITTING_METHOD_KEY,
				QASettingActivity_.FITTING_METHOD_ONE_ZERO);
	}

	public void setConcUnit(int value) {
		editor.putInt(QA_CONC_UNIT_KEY, value);
		editor.commit();
	}

	public int getConcUnit() {
		return sp.getInt(QA_CONC_UNIT_KEY,
				QASettingActivity_.CONCENTRATION_UNIT_UG_ML);
	}

	public void setCalcType(int value) {
		editor.putInt(QA_CALC_TYPE_KEY, value);
		editor.commit();
	}

	public int getCalcType() {
		return sp.getInt(QA_CALC_TYPE_KEY, QASettingActivity_.CALC_TYPE_SAMPLE);
	}

	public void setSampleNum(int value) {
		editor.putInt(QA_SAMPLE_NUM_KEY, value);
		editor.commit();
	}

	public int getSampleNum() {
		return sp.getInt(QA_SAMPLE_NUM_KEY, 1);
	}

	public void setWavelengthNum(int value) {
		editor.putInt(QA_WAVELENGTH_NUM_KEY, value);
		editor.commit();
	}

	public int getWavelengthNum() {
		return sp.getInt(QA_WAVELENGTH_NUM_KEY,
				QASettingActivity_.WAVELENGTH_ONE);
	}

	public void setK0(float value) {
		editor.putFloat(QA_K0_KEY, value);
		editor.commit();
	}

	public float getK0() {
		return sp.getFloat(QA_K0_KEY, 1.00f);
	}

	public void setK1(float value) {
		editor.putFloat(QA_K1_KEY, value);
		editor.commit();
	}

	public float getK1() {
		return sp.getFloat(QA_K1_KEY, 1.00f);
	}

	public void setK2(float value) {
		editor.putFloat(QA_K2_KEY, value);
		editor.commit();
	}

	public float getK2() {
		return sp.getFloat(QA_K2_KEY, 1.00f);
	}

	public void setK3(float value) {
		editor.putFloat(QA_K3_KEY, value);
		editor.commit();
	}

	public float getK3() {
		return sp.getFloat(QA_K3_KEY, 1.00f);
	}

	public void setWavelength1(float value) {
		editor.putFloat(QA_WAVELENGTH1_KEY, value);
		editor.commit();
	}

	public float getWavelength1() {
		return sp.getFloat(QA_WAVELENGTH1_KEY, 0.00f);
	}

	public void setWavelength2(float value) {
		editor.putFloat(QA_WAVELENGTH2_KEY, value);
		editor.commit();
	}

	public float getWavelength2() {
		return sp.getFloat(QA_WAVELENGTH2_KEY, 0.00f);
	}

	public void setWavelength3(float value) {
		editor.putFloat(QA_WAVELENGTH3_KEY, value);
		editor.commit();
	}

	public float getWavelength3() {
		return sp.getFloat(QA_WAVELENGTH3_KEY, 0.00f);
	}

	public void setRatio1(float value) {
		editor.putFloat(QA_RATIO1_KEY, value);
		editor.commit();
	}

	public float getRatio1() {
		return sp.getFloat(QA_RATIO1_KEY, 0.00f);
	}

	public void setRatio2(float value) {
		editor.putFloat(QA_RATIO2_KEY, value);
		editor.commit();
	}

	public float getRatio2() {
		return sp.getFloat(QA_RATIO2_KEY, 0.00f);
	}

	public void setRatio3(float value) {
		editor.putFloat(QA_RATIO3_KEY, value);
		editor.commit();
	}

	public float getRatio3() {
		return sp.getFloat(QA_RATIO3_KEY, 0.00f);
	}

	public void resetQA() {
		setFittingMethod(QASettingActivity_.FITTING_METHOD_ONE_ZERO);
		setConcUnit(QASettingActivity_.CONCENTRATION_UNIT_UG_ML);
		setCalcType(QASettingActivity_.CALC_TYPE_SAMPLE);
		setSampleNum(1);
		setWavelengthNum(QASettingActivity_.WAVELENGTH_ONE);
		setK0(1.00f);
		setK1(1.00f);
		setK2(1.00f);
		setK3(1.00f);
		setWavelength1(600.0f);
		setWavelength2(600.0f);
		setWavelength3(600.0f);
		setRatio1(0.00f);
		setRatio2(0.00f);
		setRatio3(0.00f);
	}

	// time scanningi setting
	public void setTestMode(int testMode) {
		editor.putInt(TS_TEST_MODE, testMode);
		editor.commit();
	}

	public int getTestMode() {
		return sp.getInt(TS_TEST_MODE, TSSettingActivity_.TEST_MODE_ABS);
	}

	public void setWavelength(float wl) {
		editor.putFloat(TS_WAVE_LENGTH, wl);
		editor.commit();
	}

	public float getWavelength() {
		return sp.getFloat(TS_WAVE_LENGTH, 600.0f);
	}

	public void setStartTime(int value) {
		editor.putInt(TS_START_TIME, value);
		editor.commit();
	}

	public int getStartTime() {
		return sp.getInt(TS_START_TIME, 0);
	}

	public void setEndTime(int value) {
		editor.putInt(TS_END_TIME, value);
		editor.commit();
	}

	public int getEndTime() {
		return sp.getInt(TS_END_TIME, 0);
	}

	public void setInterval(float value) {
		editor.putFloat(TS_INTERVAL, value);
		editor.commit();
	}

	public float getInterval() {
		return sp.getFloat(TS_INTERVAL, 1.0f);
	}

	public void setUpLimit(float value) {
		editor.putFloat(TS_UP_LIMIT, value);
		editor.commit();
	}

	public float getUpLimit() {
		return sp.getFloat(TS_UP_LIMIT, 3.00f);
	}

	public void setDownLimit(float value) {
		editor.putFloat(TS_DOWN_LIMIT, value);
		editor.commit();
	}

	public float getDownLimit() {
		return sp.getFloat(TS_DOWN_LIMIT, 0.00f);
	}

	public void reset_ts_setting() {
		setTestMode(TSSettingActivity_.TEST_MODE_ABS);
		setWavelength(600.0f);
		setStartTime(0);
		setEndTime(1000);
		setInterval(1.0f);
		setUpLimit(3.00f);
		setDownLimit(0.00f);
	}

	// wavelength scanning setting
	public void setWSUpLimit(float value) {
		editor.putFloat(WS_UP_LIMIT, value);
		editor.commit();
	}

	public float getWSUpLimit() {
		return sp.getFloat(WS_UP_LIMIT, 3.00f);
	}

	public void setWSDownLimit(float value) {
		editor.putFloat(WS_DOWN_LIMIT, value);
		editor.commit();
	}

	public float getWSDownLimit() {
		return sp.getFloat(WS_DOWN_LIMIT, 0.00f);
	}

	public void setWSInterval(float value) {
		editor.putFloat(WS_WAVELENGTH_INTERVAL, value);
		editor.commit();
	}

	public float getWSInterval() {
		return sp.getFloat(WS_WAVELENGTH_INTERVAL, 0.2f);
	}

	public void setWSTestMode(int testMode) {
		editor.putInt(WS_TEST_MODE, testMode);
		editor.commit();
	}

	public int getWSTestMode() {
		return sp.getInt(WS_TEST_MODE, WSSettingActivity_.TEST_MODE_ABS);
	}

	public void setWSStartWl(float value) {
		editor.putFloat(WS_START_WAVELENGTH, value);
		editor.commit();
	}

	public float getWSStartWl() {
		return sp.getFloat(WS_START_WAVELENGTH, 350.0f);
	}

	public void setWSEndWl(float value) {
		editor.putFloat(WS_END_WAVELENGTH, value);
		editor.commit();
	}

	public float getWSEndWl() {
		return sp.getFloat(WS_END_WAVELENGTH, 1100.0f);
	}

	public void reset_ws_setting() {
		setWSTestMode(WSSettingActivity_.TEST_MODE_ABS);
		setWSUpLimit(3.0f);
		setWSDownLimit(0.0f);
		setWSStartWl(350.0f);
		setWSEndWl(1100.0f);
		setWSInterval(0.2f);
	}

	// baseline
	public boolean isBaselineCreated() {
		return sp.getBoolean("BASELINE_CREATE", false);
	}

	public void setBaselineCreated() {
		editor.putBoolean("BASELINE_CREATE", true);
		editor.commit();
	}

	public void saveBaseline(int[] baseline) {
		JSONArray jsonArray = new JSONArray();
		for (int val : baseline) {
			jsonArray.put(val);
		}
		editor.putString(BASELINE_KEY, jsonArray.toString());
		editor.commit();
	}

	public int[] getBaseline() {
		int[] resArray = new int[BASELINE_LENGTH];
		Arrays.fill(resArray, 0);
		try {
			JSONArray jsonArray = new JSONArray(
					sp.getString(BASELINE_KEY, "[]"));
			for (int i = 0; i < jsonArray.length(); i++) {
				resArray[i] = jsonArray.getInt(i);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resArray;
	}

	public void saveBtName(String name) {
		editor.putString(BT_NAME_KEY, name);
		editor.commit();
	}

	public String getBtName() {
		return sp.getString(BT_NAME_KEY, "null");
	}

	public void saveBtAddress(String address) {
		editor.putString(BT_ADDRESS_KEY, address);
		editor.commit();
	}

	public String getBtAddress() {
		return sp.getString(BT_ADDRESS_KEY, "null");
	}

	public void setLowAccuracy() {
		setAccuracy(LOW_ACC);
	}

	public void setHighAccuracy() {
		setAccuracy(HIGH_ACC);
	}

	public boolean isLowAccuracy() {
		if (getAccuracy() == LOW_ACC) {
			return true;
		} else {
			return false;
		}
	}

	private void setAccuracy(int acc) {
		editor.putInt(ACCURACY, acc);
		editor.commit();
	}

	private int getAccuracy() {
		return sp.getInt(ACCURACY, LOW_ACC);
	}
}
